******************************************************************
* This file prepares the main analysis data for the
* Cartoon Crisis analysis, "hierarchy_cartoon_data"
******************************************************************
* Use firm-level trade data to measure exposure to the Cartoon 
* Crisis. Construct measures of Chinese import competition.
* Add firm characteristics from IV analysis and predict delayering
* at the onset of the boycott. Define main sample for Cartoon
* event study and regression analysis.
******************************************************************

clear all
set more off

cd "${data_dir}"
use tradedata.dta, clear

* CPI deflator for trade values
gen CPI = 0

replace CPI = 63.2 if aar == "1991"
replace CPI = 64.5 if aar == "1992"
replace CPI = 65.3 if aar == "1993"
replace CPI = 66.6 if aar == "1994"
replace CPI = 68.0 if aar == "1995"
replace CPI = 69.5 if aar == "1996"
replace CPI = 71 if aar == "1997"
replace CPI = 72.3 if aar == "1998"
replace CPI = 74.1 if aar == "1999"
replace CPI = 76.2 if aar == "2000"
replace CPI = 78 if aar == "2001"
replace CPI = 79.9 if aar == "2002"
replace CPI = 81.6 if aar == "2003"
replace CPI = 82.5 if aar == "2004"
replace CPI = 84 if aar == "2005"
replace CPI = 85.6 if aar == "2006"
replace CPI = 87.1 if aar == "2007"
replace CPI = 90.1 if aar == "2008"
replace CPI = 91.2 if aar == "2009"
replace CPI = 93.3 if aar == "2010"
replace CPI = 95.9 if aar == "2011"
replace CPI = 98.2 if aar == "2012"
replace CPI = 99.0 if aar == "2013"
replace CPI = 99.6 if aar == "2014"
replace CPI = 100 if aar == "2015"
replace CPI = 100.3 if aar == "2016"

* base year is 2000
replace CPI = CPI/76.2
replace vrd = vrd / CPI

destring aar, replace

* Imports from China
***************************
preserve
keep if ie == "1"
gen imp_china = vrd if land=="CN"

collapse (sum) vrd imp_china, by(aar cvrnr)

replace vrd = vrd/(8.09*1000000)
replace imp_china = imp_china/(8.09*1000000)
rename vrd imports
save imports_china.dta, replace
restore

*** Import Penetration from China
****************************************************
preserve
keep if ie == "1"
gen hs6 = substr(vare,1,6)

gen imp_china = vrd if land=="CN"

collapse (sum) vrd imp_china, by(aar hs6)

replace vrd = vrd/(8.09*1000000)
replace imp_china = imp_china/(8.09*1000000)
rename vrd imports
save imports_china_hs6.dta, replace
restore

* Save import time series data for descriptives
preserve
collapse (sum) vrd, by(cvrnr aar vare land)
destring aar, replace
save firmdata_exports_long.dta, replace
restore

* Focus on Exports now:
keep if ie == "2"

preserve
gen hs6 = substr(vare,1,6)

collapse (sum) vrd, by(cvrnr aar hs6)

merge n:1 aar hs6 using imports_china_hs6.dta
drop if _merge == 2
drop _merge

replace imports = 0 if imports == .
replace imp_china = 0 if imp_china == .

* Weights by firm exports:
sort cvrnr aar
by cvrnr aar: egen totalexp = total(vrd)
gen weight = vrd / totalexp
replace weight = 0 if weight==.

* Compute import penetration as 
* 1) share of total imports
gen impshare_china = weight * (imp_china/imports)

* 2) level of imports from China:
cap drop implevel_china
gen implevel_china = weight * log(1000000*imp_china)

collapse (sum) impshare_china implevel_china, by(cvrnr aar)

save importpenetration_china.dta, replace
restore


****************************************************
*** Cartoon Crisis: Exposure countries
****************************************************

* Define all Muslim countries if >50% population Muslims
gen Muslimtrade = 0
foreach iso2 in  SA BH KW OM QA AE ///
AF AL AZ BD BN BF TD KM DJ EG GM GN ID IR IQ JO KG KZ LB LY ///
MY MV YT ML MR MA NE NG PK PS SL SN SO SD SY TJ TN TR TM UZ EH YE {
replace Muslimtrade = 1 if (land == "`iso2'")
}

gen Muslimtradevalue = Muslimtrade*vrd

* Define labels for Muslim Countries

* First define a numerical variable:
gen muslimcountry = 1 if land == "AE"
replace muslimcountry = 2 if land == "AF"
replace muslimcountry = 3 if land == "AL"
replace muslimcountry = 4 if land == "AZ"
replace muslimcountry = 5 if land == "BD"
replace muslimcountry = 6 if land == "BF"
replace muslimcountry = 7 if land == "BH"
replace muslimcountry = 8 if land == "BN"
replace muslimcountry = 9 if land == "DJ"
replace muslimcountry = 10 if land == "EG"
replace muslimcountry = 11 if land == "EH"
replace muslimcountry = 12 if land == "GM"
replace muslimcountry = 13 if land == "GN"
replace muslimcountry = 14 if land == "ID"
replace muslimcountry = 15 if land == "IQ"
replace muslimcountry = 16 if land == "IR"
replace muslimcountry = 17 if land == "JO"
replace muslimcountry = 18 if land == "KG"
replace muslimcountry = 19 if land == "KM"
replace muslimcountry = 20 if land == "KW"
replace muslimcountry = 21 if land == "KZ"
replace muslimcountry = 22 if land == "LB"
replace muslimcountry = 23 if land == "LY"
replace muslimcountry = 24 if land == "MA"
replace muslimcountry = 25 if land == "ML"
replace muslimcountry = 26 if land == "MR"
replace muslimcountry = 27 if land == "MV"
replace muslimcountry = 28 if land == "MY"
replace muslimcountry = 29 if land == "NE"
replace muslimcountry = 30 if land == "NG"
replace muslimcountry = 31 if land == "OM"
replace muslimcountry = 32 if land == "PK"
replace muslimcountry = 33 if land == "PS"
replace muslimcountry = 34 if land == "QA"
replace muslimcountry = 35 if land == "SA"
replace muslimcountry = 36 if land == "SD"
replace muslimcountry = 37 if land == "SL"
replace muslimcountry = 38 if land == "SN"
replace muslimcountry = 39 if land == "SO"
replace muslimcountry = 40 if land == "SY"
replace muslimcountry = 41 if land == "TD"
replace muslimcountry = 42 if land == "TJ"
replace muslimcountry = 43 if land == "TM"
replace muslimcountry = 44 if land == "TN"
replace muslimcountry = 45 if land == "TR"
replace muslimcountry = 46 if land == "UZ"
replace muslimcountry = 47 if land == "YE"
replace muslimcountry = 48 if land == "YT"

label define muslimcountries ///
1 "United Arab Emirates" ///
2 "Afghanistan" ///
3 "Albania" ///
4 "Azerbaijan" ///
5 "Bangladesh" ///
6 "Burkina Faso" ///
7 "Bahrain" ///
8 "Brunei" ///
9 "Djibouti" ///
10 "Egypt" ///
11 "Western Sahara" ///
12 "Gambia" ///
13 "Guinea" ///
14 "Indonesia" ///
15 "Iraq" ///
16 "Iran" ///
17 "Jordan" ///
18 "Kyrgyzstan" ///
19 "Comoros" ///
20 "Kuwait" ///
21 "Kazakhstan" ///
22 "Lebanon" ///
23 "Lybia" ///
24 "Morocco" ///
25 "Mali" ///
26 "Mauritania" ///
27 "Maldives" ///
28 "Malaysia" ///
29 "Niger" ///
30 "Nigeria" ///
31 "Oman" ///
32 "Pakistan" ///
33 "Palestine" ///
34 "Qatar" ///
35 "Saudi Arabia" ///
36 "Sudan" ///
37 "Sierra Leone" ///
38 "Senegal" ///
39 "Somalia" ///
40 "Syria" ///
41 "Chad" ///
42 "Tajikistan" ///
43 "Turkmenistan" ///
44 "Tunisia" ///
45 "Turkey" ///
46 "Uzbekistan" ///
47 "Yemen" ///
48 "Mayotte"

label values muslimcountry muslimcountries

* Save time series data for descriptives
preserve
collapse (sum) vrd Muslimtradevalue, by(cvrnr aar vare land muslimcountry)
destring aar, replace
save firmdata_exports_long.dta, replace
restore

* Next, conduct firm-level analysis:
**************************************
collapse (sum) Arabtradevalue Muslimtradevalue vrd, by(aar cvrnr)

* By exporting and importing:
gen Arabdummy = (Arabtradevalue > 0 & Arabtradevalue != .)
gen Muslimdummy = (Muslimtradevalue > 0 & Muslimtradevalue != .)

* Firms that never sell to Muslim destinations:
bys aar cvrnr: egen Muslimany = max(Muslimdummy)

* add firm characteristics:
sort aar cvrnr
merge 1:1 aar cvrnr using "firm_characteristics.dta"

tab aar _merge
drop if _merge == 2
drop _merge

* Prepare panel analysis
***************************
destring aar, gen(year)
drop if year == . | cvrnr == ""
egen firmcode = group(cvrnr)
xtset firmcode year

* Real values in USD
**********************************************
foreach var in vrd Muslimtradevalue Arabtradevalue sales {
replace `var' = `var'/(1000000*8.09)
}
gen logsales_usd = log(sales)

* Define treatment group: 
****************************************

* Relative importance of Muslim exports in 2005:
sort cvrnr aar
gen Arabexphelp = Muslimtradevalue/vrd if year == 2005
by cvrnr: egen Arabshare = max(Arabexphelp)

gen Arab2005 = (Muslimdummy==1) * (year==2005) ///
if (year==2003 | year==2004 | year==2005) & Muslimdummy != .
bys cvrnr: egen Arabpre = max(Arab2005)

* Drop firms with less than 0.5% exports to Muslim countries from the analysis.
cap drop treatment
gen treatment = 1 if Arabpre == 1 & Arabshare > 0.005
replace treatment = 0 if Arabpre == 0

cap drop treatment_trend
gen treatment_trend = (aar - 1999)* treatment

* Table A4
tab aar treatment if sample == 1
tab layers Arab2005 if year == 2005


* Interactions by period
****************************************
gen Arab20052001 = treatment * (year == 2001)
gen Arab20052002 = treatment * (year == 2002)
gen Arab20052003 = treatment * (year == 2003)
gen Arab20052004 = treatment * (year == 2004)
gen Arab20052005 = treatment * (year == 2005)
gen Arab20052006 = treatment * (year == 2006)
gen Arab20052007 = treatment * (year == 2007)
gen Arab20052008 = treatment * (year == 2008)

forvalues x = 1999/2008{
gen year`x' = year == `x'
}

* Main Sample from IV analysis
destring aar, replace
sort aar cvrnr
merge 1:1 aar cvrnr using "hierarchy_IV_data", keepusing(sample)
drop if _merge == 2
drop _merge

* Define consistent subsamples for cartoon analysis
xtset firmcode aar

* high risk sample
gen highrisksample = D.layers != . ///
& sample == 1 & D.logsales_usd != . & L.layers >= 2 ///
& wagegap3_topbottom_log > 0 & wagegap3_topbottom_log < 1

* full sample 
gen fullsample = sample == 1 & D.layers != . & ///
D.logsales_usd != .  & treatment != .

xtset firmcode aar


* Predict Propensity to Delayer
**************************************
gen logempl = log(empl2)
destring aar, replace
sort aar cvrnr 
merge 1:1 aar cvrnr using "fire.dta", keepusing(akg kgl xlg at)
drop if _merge == 2
drop _merge
gen totalleverage = (akg + kgl + xlg) / at

logit droplayers ///
L.c.logempl L.c.logsales_usd##L.i.layers ///
L.addlayers L.D.logempl L.totalleverage ///
if aar == 2002 & sample == 1

cap drop prdrop
predict prdrop, pr
sum prdrop if L.layers == 1
* mechanically, the probability is zero for 1-layer firms; 
* they will be dropped in the aanalysis later
replace prdrop = 0 if L.layers == 1

cap drop exratiohelp
gen exratiohelp = prdrop  if aar == 2006
cap drop prdrop2005
bys cvrnr: egen prdrop2005 = max(exratiohelp)
replace prdrop2005 = 0 if prdrop2005 == .

set more off
drop if sector027 == . | cvrnr == ""
cap drop firmcode_xt
egen firmcode_xt = group(cvrnr sector027)
xtset firmcode_xt aar


* Define prdrop sample 
gen prdropsample = & D.layers != . & D.logsales_usd != . ///
& D.log2inequality4 != . & L.layers >= 2 & sample == 1

save hierarchy_cartoon_data, replace

